﻿import QtQuick 2.5

Flickable {
    id:rootId
    width: 600; height: 500
    contentWidth:600; contentHeight: 1800
    //瀑布流
    property bool loadFinshed: false
    property int perCount: 3
    property string myUrl : qsTr("file:///D:/examples/MixQmlWidget/images/1 (%1).gif")

    ListModel{
        id:picModel
    }
    Repeater {
        id:rp
        model: picModel
        Rectangle{
            width: rootId.width/perCount
            height: Math.min(img.sourceSize.height,200)
            border.width: 2
            border.color:Qt.rgba(Math.random(),Math.random(),Math.random(),1.0)
            radius: 2
            AnimatedImage {
                id:img
                anchors.fill: parent
                anchors.margins: 2
                fillMode: Image.PreserveAspectCrop
                playing: ma.containsMouse
                currentFrame: 3
                source:  url
                MouseArea{
                    id:ma
                    anchors.fill: parent
                    hoverEnabled: true
                }
            }
        }
    }
    function change(){
        var lis=rp;
        var lis_W= rootId.width/perCount;
        var windowCW = rootId.width;
        var n = Math.floor(windowCW/lis_W);
        var center = (windowCW - n*lis_W-(n-1)*10)/2;
        var arrH = [];
        console.log("center=====",center);
        for(var i=0;i<lis.count;i++){
            var j=i%n;
            if (arrH.length==n) {
                var min = findMin(arrH);
                lis.itemAt(i).x =center + min*(lis_W+10) ;
                lis.itemAt(i).y = arrH[min]+10 ;
                arrH[min] += lis.itemAt(i).height + 10;

                var k=0;
                for(var b=0;b<n;b++){
                    k=Math.max(arrH[k],arrH[b])==arrH[k] ? k : b;
                }
                //console.log("height=====",arrH[k]);
                rootId.contentHeight = arrH[k];
            }else{
                arrH[j] = lis.itemAt(i).height ;
                lis.itemAt(i).x =center + lis_W*j+10*j ;
                lis.itemAt(i).y = 0;
            }
        }
        loadFinshed = true;
    }
    function findMin(arr) {
        var m = 0;
        for (var i = 0; i < arr.length; i++) {
            m = Math.min(arr[m], arr[i]) == arr[m] ? m : i;
        }
        return m;
    }

    Component.onCompleted: {
        for(var i=0;i<15;i++){
            picModel.append({url:myUrl.arg(i+1)});
        }
        change();
    }
    onWidthChanged: {
        change()
    }
    onHeightChanged: {
        change()
    }
    onContentYChanged: {
        // 下拉刷新判断逻辑：已经到头了，还下拉一定距离
        if (contentY < originY){
            var dy = contentY - originY;
            if (dy < -14){
                //needReflesh = true;
            }
        }
        // 上拉加载判断逻辑：已经到底了，还上拉一定距离
        if (contentHeight>height && contentY-originY > contentHeight-height){
            var dy1 = (contentY-originY) - (contentHeight-height);
            if (dy1 > 8){
                console.log("load more loadFinshed ",loadFinshed);
                if(loadFinshed){
                    loadFinshed = false;
                    var cnt = picModel.count;
                    for(var i=0;i<3;i++){
                        picModel.append({url:myUrl.arg(cnt+i)});
                    }
                    change();
                }
            }
        }
    }
}
